package ru.reaperoq.inventory.screens.main.admin.locations

import LoadingStatus
import LocationsViewModel
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.ListItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import models.LocationUi
import models.LocationsEvent
import org.koin.androidx.compose.koinViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LocationsScreen(
    modifier: Modifier = Modifier,
    viewModel: LocationsViewModel = koinViewModel()
) {
    val viewState by viewModel.viewStates().collectAsState()

    var showAddDialog by remember { mutableStateOf(false) }

    if (showAddDialog) {
        AddLocationDialog(
            onDismissRequest = { showAddDialog = false },
            onConfirm = { name ->
                viewModel.obtainEvent(LocationsEvent.AddLocation(name))
                showAddDialog = false
            }
        )
    }

    LaunchedEffect(Unit) {
        viewModel.obtainEvent(LocationsEvent.LoadLocations)
    }

    Scaffold(
        modifier = modifier
            .statusBarsPadding(),
        topBar = {
            CenterAlignedTopAppBar(title = { Text("Локации") })
        },
        floatingActionButton = {
            FloatingActionButton(onClick = {
                showAddDialog = true
            }) {
                Icon(imageVector = Icons.Default.Add, contentDescription = null)
            }
        }
    ) { padding ->
        Column(
            modifier = Modifier
                .consumeWindowInsets(padding)
                .padding(padding)
                .fillMaxSize(),
        ) {
            if (viewState.loadingStatus is LoadingStatus.Loading) {
                LinearProgressIndicator(Modifier.fillMaxWidth())
            }
            LocationsList(
                locations = viewState.locations,
                onDeleteClick = { location: LocationUi ->
                    viewModel.obtainEvent(LocationsEvent.DeleteLocation(location.id))
                }
            )
        }
    }
}

@Composable
fun LocationsList(
    modifier: Modifier = Modifier,
    locations: List<LocationUi>,
    onDeleteClick: (LocationUi) -> Unit
) {
    LazyColumn(modifier = modifier) {
        items(locations, key = { it.id }) { location ->
            LocationItem(location = location, onDeleteClick = onDeleteClick)
        }
    }
}

@Composable
fun LocationItem(
    modifier: Modifier = Modifier,
    location: LocationUi,
    onDeleteClick: (LocationUi) -> Unit
) {
    ListItem(
        modifier = modifier,
        headlineContent = { Text(text = location.name) },
        trailingContent = {
            IconButton(onClick = { onDeleteClick(location) }) {
                Icon(imageVector = Icons.Default.Delete, contentDescription = null)
            }
        })
}